package biz.alla.market.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import biz.alla.market.dao.CategoryDao;
import biz.alla.market.domain.Category;

public class CategoryDaoImpl extends HibernateDaoSupport implements CategoryDao {

	@Override
	public int create(Category category) {
		return (Integer) getHibernateTemplate().save(category);
	}

	@Override
	public List<Category> get() {
		List<Category> categoriesSet = new ArrayList<Category>();
		categoriesSet.addAll(getHibernateTemplate().loadAll(Category.class));

		return categoriesSet;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Category find(String name) {
		List categories = getHibernateTemplate().find(
				"from Category where name='" + name + "'");
		return categories != null && !categories.isEmpty() ? (Category) categories
				.get(0) : null;
	}

	@Override
	public void update(Category category) {
		getHibernateTemplate().update(category);
	}

	@Override
	public void delete(Category category) {
		getHibernateTemplate().delete(category);
	}

}
